/*
 * @Description: 钱包   Wallet
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-29 14:32:53
 * @LastAuthor: zhengzhuang
 * @lastTime: 2019-12-10 13:25:20
 */
import 'dart:ui';
import 'package:C20Driver/model/driver_daily_income_model.dart';
import 'package:C20Driver/model/income_entity.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/request.dart';

class Wallet extends StatefulWidget {
  @override
  _WalletState createState() => _WalletState();
}

class _WalletState extends State<Wallet> with SingleTickerProviderStateMixin {
  // TabController mTabController;
  String _money = '0.00'; // 我的余额
  String _allmoney = '0.00'; // 我的全部余额
  String _freezeBalance = '0'; // 可用余额-不可提现
  String _bankLeaveMobile = '****'; // 银行预留手机号
  String _sumPrice = '0.00'; // 今日流水
  /// 流水时间
  String _recordTime = '';
  /// 流水时间传参数
  String _numRecordTime = '';
  int _year = 1900;
  int _month;
  int _day;
  @override
  void initState() {
    super.initState();
    // mTabController = TabController(vsync: this, length: 3);
    getTodayData();
    _getBalance();
    _getAllBalance();
    _getFreezeBalance();
  }

  /// 我的全部余额
  void _getAllBalance() async {
    var data = await Request.getAllBalance();
    setState(() {
      _allmoney = data;
    });
  }
  /// 获取今天日期
  void getTodayData() {
    DateTime now = new DateTime.now();
    var monthStr="";
    var dayStr="";

    var mouth=now.month;
    var day=now.day;
    if(mouth<10){
      monthStr='0$mouth';
    }else{
      monthStr=mouth.toString();
    }
    if(day<10){
      dayStr='0$day';
    }else{
      dayStr=day.toString();
    }
    setState(() {
      _recordTime = now.year.toString() +
          '年' +
         monthStr +
          '月' +
         dayStr +
          '日';
      _numRecordTime = now.year.toString() +
          '-' +
          monthStr +
          '-' +
          dayStr;
      _year = now.year;
      _month = now.month;
      _day = now.day;
    });
    Request.getIncomeByDay(_numRecordTime).then((value) {
      print("--------->value:$value");
      var snapshotData = DriverIncome.fromJson(value);
      setState(() {
        _sumPrice = snapshotData.sum;
        if(_sumPrice=="0"){
          _sumPrice="0.0";
        }
      });
    });
  }
  /// 获取余额
  void _getBalance() async {
    var data = await Request.getBalance();
    setState(() {
      _money = data;
    });
  }

  /// 我的资产（可用余额-不可提现）
  void _getFreezeBalance() async {
    var data = await Request.getFreezeBalance();
    setState(() {
      _freezeBalance = data;
    });
  }

  /// 获取银行卡信息
  void _getUserBankCard() async {
    var dataInfo = await Request.getUserBankCard();
    print("errrrrrrrrrr" + dataInfo['bank_reserved_mobile']);
    setState(() {
      _bankLeaveMobile = dataInfo['bank_reserved_mobile'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('我的钱包'),
          elevation: 0,
        ),
        body: Container(
          padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(16),
            top: ScreenUtil().setWidth(28),
          ),
          color: Color(0xffffffff),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Stack(
                children: [

                  Container(

                    width: ScreenUtil().setWidth(750),
                    height: ScreenUtil().setWidth(490),
                    alignment: Alignment.bottomCenter,
                    margin: EdgeInsetsDirectional.only(start: ScreenUtil().setWidth(16),end: ScreenUtil().setWidth(29) ,top:  ScreenUtil().setWidth(16)),
                    decoration: BoxDecoration(
                        color: Color(0xffE1EDFF),
                        borderRadius: BorderRadiusDirectional.all(Radius.circular(ScreenUtil().setHeight(16)))
                    ),
                    child: _statementDate(),
                  ),
                  Image.asset("assets/icon/walletbg.png"),
                  Container(
                    padding: EdgeInsets.only(
                        left: ScreenUtil().setWidth(40),
                        right: ScreenUtil().setWidth(70)),
                    width: ScreenUtil().setWidth(750),
                    height: ScreenUtil().setWidth(366),
                    decoration: BoxDecoration(
                      // image: DecorationImage(
                      //   image: AssetImage('assets/icon/walletbg.png'),
                      // ),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: ScreenUtil().setWidth(50),
                        ),
                        Text(
                          '总金额（元）',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(28),
                            color: Color(0xffffffff),
                          ),
                        ),
                        SizedBox(
                          height: ScreenUtil().setWidth(35),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              _allmoney,
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(44),
                                fontWeight: FontWeight.w500,
                                color: Color(0xffffffff),
                              ),
                            ),
                            InkWell(
                              child: Container(
                                alignment: Alignment.center,
                                width: ScreenUtil().setWidth(166),
                                height: ScreenUtil().setWidth(66),
                                decoration: BoxDecoration(
                                  border: Border.all(
                                      width: ScreenUtil().setWidth(1),
                                      color: Color(0xffffffff)),
                                  color: Colors.transparent,
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(ScreenUtil().setWidth(33)),
                                  ),
                                ),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    SizedBox(
                                      width: ScreenUtil().setWidth(10),
                                    ),
                                    Text(
                                      '去提现',
                                      style: TextStyle(
                                        color: Color(0xffffffff),
                                        fontSize: ScreenUtil().setSp(26),
                                      ),
                                    ),
                                    SizedBox(
                                      width: ScreenUtil().setWidth(10),
                                    ),
                                    Icon(
                                      Icons.chevron_right,
                                      size: ScreenUtil().setWidth(32),
                                      color: Color(0xffffffff),
                                    ),
                                  ],
                                ),
                              ),
                              onTap: () {
                                NavigatorUtil.jump(context,
                                    '/cashWithdrawal?money=$_money&type=0010');
                              },
                            ),
                          ],
                        ),
                        SizedBox(
                          height: ScreenUtil().setWidth(35),
                        ),
                        Divider(
                          color: Color(0x33ffffff),
                          height: ScreenUtil().setWidth(2),
                        ),
                        SizedBox(
                          height: ScreenUtil().setWidth(35),
                        ),
                        Text(
                          '可用余额：$_money元',
                          style: TextStyle(
                              color: Color(0xffffffff),
                              fontSize: ScreenUtil().setWidth(28),
                              fontWeight: FontWeight.w400),
                        ),
                      ],
                    ),
                  ),

                ],
              ),
              InkWell(
                onTap: () {
                  NavigatorUtil.jump(context, '/billTabDetail');
                },
                child: Container(
                  margin: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
                  width: ScreenUtil().setWidth(686),
                  height: ScreenUtil().setWidth(108),
                  decoration: BoxDecoration(
                    color: Color(0xffF6F8FC),
                    borderRadius: BorderRadius.all(
                      Radius.circular(16),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(
                          left: ScreenUtil().setWidth(32),
                        ),
                        child: Text(
                          '账单明细',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(30),
                            color: Color(0xff333333),
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(
                          right: ScreenUtil().setWidth(32),
                        ),
                        child: Icon(
                          Icons.chevron_right,
                          color: Color(0xff333333),
                          size: ScreenUtil().setWidth(40),
                        ),
                      )
                    ],
                  ),
                ),
              ),
              InkWell(
                onTap: () => NavigatorUtil.jump(context, '/myIncomeRecord'),
                child: Container(
                  margin: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
                  width: ScreenUtil().setWidth(686),
                  height: ScreenUtil().setWidth(108),
                  decoration: BoxDecoration(
                    color: Color(0xffF6F8FC),
                    borderRadius: BorderRadius.all(
                      Radius.circular(16),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(
                          left: ScreenUtil().setWidth(32),
                        ),
                        child: Text(
                          '我的流水',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(30),
                            color: Color(0xff333333),
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(
                          right: ScreenUtil().setWidth(32),
                        ),
                        child: Icon(
                          Icons.chevron_right,
                          color: Color(0xff333333),
                          size: ScreenUtil().setWidth(40),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ));
  }
  /// 我的流水顶部日期小组件
  Widget _statementDate() {
    return Container(
      padding: EdgeInsets.fromLTRB(
        ScreenUtil().setWidth(32),
        ScreenUtil().setWidth(56),
        ScreenUtil().setWidth(40),
        ScreenUtil().setWidth(18),
      ),
      alignment: Alignment.bottomCenter,

      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '今日流水(元)',
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(28.0),
                  color: Color(0xff999999),
                ),
              ),
              SizedBox(
                height: ScreenUtil().setWidth(13.0),
              ),
              Text(
                 '$_sumPrice',
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(44.0),
                  color: Color(0xff333333),
                  fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
          InkWell(
            child: Container(
              // width: ScreenUtil().setWidth(300.0),
              margin: EdgeInsetsDirectional.only(bottom: ScreenUtil().setHeight(45)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    '$_recordTime',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(32.0),
                    ),
                  ),
                  Icon(
                    Icons.arrow_drop_down,
                    color: Color(0xffcccccc),
                  ),
                ],
              ),
            ),
            onTap: () => _setTimeDate(context),
          )
        ],
      ),
    );
  }


  /// 设置查看流水日期
  void _setTimeDate(context) {
    DateTime now = new DateTime.now();
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1900, 1, 1),
        maxTime: DateTime(now.year, now.month, now.day), onConfirm: (date) {
          var monthStr="";
          var dayStr="";

          var mouth=date.month;
          var day=date.day;
          if(mouth<10){
            monthStr='0$mouth';
          }else{
            monthStr=mouth.toString();
          }
          if(day<10){
            dayStr='0$day';
          }else{
            dayStr=day.toString();
          }
          _numRecordTime = date.year.toString() +
              '-' +
        monthStr+
              '-' +
              dayStr;
          Request.getIncomeByDay(_numRecordTime).then((value) {
            var snapshotData = DriverIncome.fromJson(value);
            setState(() {
              _sumPrice = snapshotData.sum;
              if(_sumPrice=="0"){
                _sumPrice="0.0";
              }
              _recordTime = date.year.toString() +
                  '年' +
                 monthStr +
                  '月' +
                 dayStr +
                  '日';
              _numRecordTime = date.year.toString() +
                  '-' +
                  monthStr +
                  '-' +
                  dayStr;
              _year = date.year;
              _month = date.month;
              _day = date.day;
            });
          });
        }, currentTime: DateTime(_year, _month, _day), locale: LocaleType.zh);
  }


  @override
  void dispose() {
    super.dispose();
  }
}

class Choice {
  const Choice({this.title, this.icon, this.position});
  final String title;
  final int position;
  final IconData icon;
}
